package com.bite.product.controller;


import com.bite.api.ProductInteface;
import com.bite.model.ProductInfo;
import com.bite.product.service.ProductService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("product")
@RestController
public class ProductController implements ProductInteface {
    @Autowired
    ProductService productService;

//    @RequestMapping("/{productId}")
//    public ProductInfo selectProductById(@PathVariable("productId") Integer productId) {
//        log.info("productId: " + productId);
//        return productService.selectProductById(productId);
//    }

//    @RequestMapping("/{productId}")
//    public void selectProductById(@PathVariable("productId") Integer productId, String userName) {
//        System.out.println("接收到的productId: " + productId);
//        System.out.println("userName: " + userName);
//    }


    @RequestMapping("p1")
    public String p1(Integer id) {
        return "接收到的参数id: " + id;
    }

    @RequestMapping("p2")
    public String p2(Integer id, String name) {
        return "接收到的参数id: " + id + "name: " + name;
    }

    @RequestMapping("p3")
    public String p3(ProductInfo productInfo) {
        return "接受到的对象 productInfo:" + productInfo;
    }

    @RequestMapping("p4")
    public String p4(@RequestBody ProductInfo productInfo) {
        return "接受到的对象 productInfo:" + productInfo;
    }


    @SneakyThrows
    @Override
    @RequestMapping("/{productId}")
    public ProductInfo selectProductById(@PathVariable("productId") Integer productId) {
        //模拟慢响应
//        long millis = new Random().nextInt(20) + 50;
//        Thread.sleep(millis);
//        log.info("接收参数, productId:" + productId + ", sleep:" + millis);
//        return productService.selectProductById(productId);
        if (productId == 1001) {
            //模拟慢响应
            Thread.sleep(60);
            return productService.selectProductById(productId);
        } else if (productId == 1002) {
            // 模拟异常
            throw new RuntimeException("发⽣异常");
        } else {
            return productService.selectProductById(productId);
        }
    }
}
